<template>
  <div class="system-monitor">
    <div class="monitor-header">
      <h3 class="monitor-title">系统监控</h3>
      <div class="monitor-status">
        <span class="status-indicator normal">
          <span class="status-dot"></span>
          正常
        </span>
      </div>
    </div>
    
    <div class="monitor-content">
      <div class="monitor-layout">
        <!-- 左侧：系统指标 -->
        <div class="metrics-section">
          <h4 class="section-title">系统指标</h4>
          <div class="metrics-grid">
            <div 
              v-for="(metric, index) in systemMetrics" 
              :key="index"
              class="metric-card"
              :class="metric.status"
            >
              <div class="metric-header">
                <div class="metric-icon">
                  <i :class="metric.icon"></i>
                </div>
                <div class="metric-name">{{ metric.name }}</div>
              </div>
              <div class="metric-value">{{ metric.value }}{{ metric.unit }}</div>
              <div class="metric-bar">
                <div 
                  class="bar-progress" 
                  :style="{ 
                    width: metric.percentage + '%',
                    background: metric.color 
                  }"
                ></div>
              </div>
              <div class="metric-status">{{ metric.statusText }}</div>
            </div>
          </div>
        </div>
        
        <!-- 右侧：服务器状态 -->
        <div class="server-section">
          <h4 class="section-title">服务器状态</h4>
          <div class="server-list">
            <div 
              v-for="(server, index) in servers" 
              :key="index"
              class="server-item"
              :class="server.status"
            >
              <div class="server-info">
                <div class="server-name">{{ server.name }}</div>
                <div class="server-location">{{ server.location }}</div>
              </div>
              <div class="server-metrics">
                <div class="server-metric">
                  <span class="metric-label">CPU:</span>
                  <span class="metric-value">{{ server.cpu }}%</span>
                  <div class="metric-mini-bar">
                    <div class="mini-bar-fill" :style="{ width: server.cpu + '%', background: server.cpu > 80 ? '#ff4757' : server.cpu > 60 ? '#ffa502' : '#00ff9d' }"></div>
                  </div>
                </div>
                <div class="server-metric">
                  <span class="metric-label">内存:</span>
                  <span class="metric-value">{{ server.memory }}%</span>
                  <div class="metric-mini-bar">
                    <div class="mini-bar-fill" :style="{ width: server.memory + '%', background: server.memory > 80 ? '#ff4757' : server.memory > 60 ? '#ffa502' : '#00ff9d' }"></div>
                  </div>
                </div>
              </div>
              <div class="server-status-indicator">
                <span class="status-dot" :class="server.status"></span>
                {{ server.statusText }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const systemMetrics = ref([
  {
    name: 'CPU使用率',
    value: 35,
    unit: '%',
    percentage: 35,
    icon: 'fas fa-processor',
    color: '#00f0ff',
    status: 'normal',
    statusText: '正常'
  },
  {
    name: '内存使用',
    value: 68,
    unit: '%',
    percentage: 68,
    icon: 'fas fa-memory',
    color: '#ffa502',
    status: 'warning',
    statusText: '警告'
  },
  {
    name: '磁盘使用',
    value: 42,
    unit: '%',
    percentage: 42,
    icon: 'fas fa-hdd',
    color: '#00ff9d',
    status: 'normal',
    statusText: '正常'
  },
  {
    name: '网络流量',
    value: 156,
    unit: 'MB/s',
    percentage: 78,
    icon: 'fas fa-network-wired',
    color: '#00f0ff',
    status: 'normal',
    statusText: '正常'
  }
])

const servers = ref([
  {
    name: '主服务器',
    location: '北京-BGP',
    cpu: 32,
    memory: 58,
    status: 'online',
    statusText: '在线'
  },
  {
    name: '备用服务器',
    location: '上海-阿里云',
    cpu: 28,
    memory: 45,
    status: 'online',
    statusText: '在线'
  },
  {
    name: 'CDN节点',
    location: '深圳-腾讯云',
    cpu: 15,
    memory: 32,
    status: 'online',
    statusText: '在线'
  },
  {
    name: '数据库服务器',
    location: '杭州-阿里云',
    cpu: 45,
    memory: 72,
    status: 'warning',
    statusText: '繁忙'
  }
])

let updateTimer = null

const updateMetrics = () => {
  systemMetrics.value.forEach(metric => {
    // 模拟数据波动
    const change = (Math.random() - 0.5) * 10
    let newValue = metric.value + change
    
    // 限制范围
    newValue = Math.max(0, Math.min(100, newValue))
    metric.value = Math.round(newValue)
    
    if (metric.name === '网络流量') {
      metric.value = Math.round(newValue * 2) // 网络流量单位不同
      metric.percentage = Math.min(metric.value / 2, 100)
    } else {
      metric.percentage = metric.value
    }
    
    // 更新状态
    if (metric.percentage < 50) {
      metric.status = 'normal'
      metric.statusText = '正常'
      metric.color = '#00ff9d'
    } else if (metric.percentage < 80) {
      metric.status = 'warning'
      metric.statusText = '警告'
      metric.color = '#ffa502'
    } else {
      metric.status = 'danger'
      metric.statusText = '危险'
      metric.color = '#ff4757'
    }
  })
  
  // 更新服务器状态
  servers.value.forEach(server => {
    server.cpu += (Math.random() - 0.5) * 5
    server.memory += (Math.random() - 0.5) * 3
    
    server.cpu = Math.max(10, Math.min(90, server.cpu))
    server.memory = Math.max(20, Math.min(85, server.memory))
    
    server.cpu = Math.round(server.cpu)
    server.memory = Math.round(server.memory)
    
    // 更新服务器状态
    if (server.cpu > 80 || server.memory > 80) {
      server.status = 'warning'
      server.statusText = '繁忙'
    } else {
      server.status = 'online'
      server.statusText = '在线'
    }
  })
}

onMounted(() => {
  updateTimer = setInterval(updateMetrics, 2000)
})

onUnmounted(() => {
  if (updateTimer) {
    clearInterval(updateTimer)
  }
})
</script>

<style scoped>
.system-monitor {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: rgba(15, 52, 96, 0.3);
  border-radius: 12px;
  border: 1px solid rgba(0, 240, 255, 0.2);
  backdrop-filter: blur(10px);
  padding: 20px;
  overflow: hidden;
}

.monitor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(0, 240, 255, 0.1);
}

.monitor-title {
  color: #00f0ff;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.monitor-status {
  display: flex;
  align-items: center;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
}

.status-indicator.normal {
  color: #00ff9d;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  animation: pulse-glow 2s infinite;
}

.status-indicator.normal .status-dot {
  background: #00ff9d;
}

@keyframes pulse-glow {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(0, 255, 157, 0.7);
  }
  70% {
    box-shadow: 0 0 0 8px rgba(0, 255, 157, 0);
  }
}

.monitor-content {
  flex: 1;
  overflow-y: auto;
  padding-right: 8px;
}

.monitor-content::-webkit-scrollbar {
  width: 4px;
}

.monitor-content::-webkit-scrollbar-track {
  background: rgba(0, 240, 255, 0.1);
  border-radius: 2px;
}

.monitor-content::-webkit-scrollbar-thumb {
  background: rgba(0, 240, 255, 0.3);
  border-radius: 2px;
}

.monitor-layout {
  display: flex;
  gap: 20px;
  height: 100%;
}

.metrics-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.server-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.section-title {
  color: #00f0ff;
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 16px 0;
  text-align: center;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0, 240, 255, 0.2);
}

.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  flex: 1;
}

.metric-card {
  background: rgba(0, 240, 255, 0.05);
  border-radius: 8px;
  padding: 12px;
  border: 1px solid rgba(0, 240, 255, 0.1);
  transition: all 0.3s ease;
}

.metric-card:hover {
  background: rgba(0, 240, 255, 0.1);
  border-color: rgba(0, 240, 255, 0.3);
  transform: translateY(-2px);
}

.metric-card.warning {
  border-color: rgba(255, 165, 2, 0.3);
}

.metric-card.danger {
  border-color: rgba(255, 71, 87, 0.3);
}

.metric-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.metric-icon {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #00f0ff;
  font-size: 12px;
}

.metric-name {
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  font-weight: 500;
}

.metric-value {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}

.metric-bar {
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 6px;
}

.bar-progress {
  height: 100%;
  border-radius: 2px;
  transition: width 0.5s ease;
}

.metric-status {
  color: rgba(255, 255, 255, 0.6);
  font-size: 10px;
}

.server-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
}

.server-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: rgba(0, 240, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(0, 240, 255, 0.1);
  transition: all 0.3s ease;
}

.server-item:hover {
  background: rgba(0, 240, 255, 0.1);
  border-color: rgba(0, 240, 255, 0.3);
}

.server-item.warning {
  border-color: rgba(255, 165, 2, 0.3);
}

.server-info {
  flex: 1;
}

.server-name {
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
}

.server-location {
  color: rgba(255, 255, 255, 0.6);
  font-size: 10px;
}

.server-metrics {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.server-metric {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: space-between;
}

.metric-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 10px;
  font-weight: 500;
}

.metric-value {
  color: rgba(255, 255, 255, 0.8);
  font-size: 10px;
}

.metric-mini-bar {
  width: 60px;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
}

.mini-bar-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.5s ease;
}

.server-status-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 10px;
  min-width: 40px;
}

.server-status-indicator .status-dot {
  width: 6px;
  height: 6px;
}

.server-status-indicator .status-dot.online {
  background: #00ff9d;
}

.server-status-indicator .status-dot.warning {
  background: #ffa502;
}
</style> 